﻿using System;
using System.Collections.Generic;
using System.Reflection;
using Game.modules.properties;
using GameFramework.common;
using UnityEngine;

namespace GameFramework.scripts
{
    /// <summary>
    /// 属性计算脚本管理器
    /// </summary>
    public class PropertyCalScriptManager:Singleton<PropertyCalScriptManager>
    {
        //功能对应的属性计算脚本，key：功能id，value：脚本
        private Dictionary<int, IPropertyCalScript> _scripts = new Dictionary<int, IPropertyCalScript>();

        public void loadScripts()
        {
            Type scriptType = typeof(IPropertyCalScript);
            Type[] types = Assembly.GetExecutingAssembly().GetTypes();
            foreach (Type type in types)
            {
                if (type.IsInterface)
                {
                    continue;
                }

                if (!scriptType.IsAssignableFrom(type))
                {
                    continue;
                }

                IPropertyCalScript instance = Activator.CreateInstance(type) as IPropertyCalScript;
                if (instance != null)
                {
                    int id = instance.getId();
                    if (_scripts.ContainsKey(id))
                    {
                        IPropertyCalScript old = _scripts[id];
                        Debug.LogError("属性处理脚本重复，脚本id:"+id+" 脚本名："+type.Name+" 容器中的脚本名："+old.GetType().Name);
                        continue;
                    }
                    _scripts.Add(id,instance);
                }
            }
        }

        public IPropertyCalScript getScript(int FuncId)
        {
            if (_scripts.ContainsKey(FuncId))
            {
                return _scripts[FuncId];
            }

            return null;
        }
    }
}